<?php
/**
 * Created by PhpStorm.
 * User: Inhere
 * Date: 2015/1/29
 * Use : ...
 * File: JsonHelper.php
 */

namespace ulue\libs\helpers;

class JsonHelper
{
    /**
     * @var array|null
     */
    protected $data = null;

    /**
     * @var string|null
     */
    protected $json = '';

    protected $inFile;

    protected $outPath;

    protected $suffix='.json';

    public function __construct(array $options=[])
    {
        if ($options) {
            isset($options['inFile']) && $this->inFile = $options['inFile'];
            isset($options['outPath']) && $this->outPath = $options['outPath'];
            isset($options['data']) && $this->data = (array)$options['data'];
        }
    }

    public function loadString($string, $clear=true)
    {
        $this->json = trim($string);

        if ($clear) {
            $this->json = $this->clear($this->json);
        }

        return $this;
    }

    public function loadFile($file, $clear=true)
    {
        if (!file_exists($file)) {
            throw new \DNotFoundException("没有找到或不存在资源文件{$file}");
        }

        $this->inFile = $file;

        $this->data = $data = file_get_contents($file);

        return $this->loadString($data, $clear);
    }

    public function save($type='min')
    {
        $name  = basename($this->inFile, '.json');

        if (!is_dir($this->outPath)) {
            $this->outPath = dirname($this->inFile);
        }

        if ( $type == 'min' )
        {
            // 去掉空白
            $data = preg_replace('/(?!\w)\s*?(?!\w)/is', '',$this->json);
        }

        $file  = $this->outPath . '/' . $name . '.' . $type . $this->suffix;

        @file_put_contents($file, $data);
    }

    public function saveAs($output, $type='min')
    {
        $name  = basename($this->inFile, '.json');

        if (is_dir($output)) {
            $this->outPath = $output;
        } elseif (is_file($output)) {
            $this->outPath = dirname($output);
            $name  = basename($output, '.json');
        } else{
            trigger_error('param 1 error!');
        }

        if ( !file_exists($this->outPath) ) {
            trigger_error('设置的json文件输出'.$this->outPath.'目录不存在！');
        }

        $file  = $this->outPath . '/' . $name . '.' . $type . $this->suffix;

        if ( $type == 'min' )
        {
            // 去掉空白
            $data = preg_replace('/(?!\w)\s*?(?!\w)/is', '',$data);
        }


        @file_put_contents($file, $data);

    }

    public function clear($string, $type='raw')
    {

        $json = preg_replace(array(

            // 去掉所有多行注释/* .... */
            '/\/\*.*?\*\/\s*/is',

            // 去掉所有单行注释//....
            '/\/\/.*?[\r\n]/is',

            // 去掉空白行
            "/(\n[\r])+/is"

        ),  array("",'',' '), $string);

        if ( $type == 'min' )
        {
            // 去掉空白
            $json = preg_replace('/(?!\w)\s*?(?!\w)/is', '',$json);
        }

        return trim($json);
    }

    public function setData()
    {
        $this->data = $data;

        return $this;
    }

    public function getData()
    {
        return $this->data;
    }

    public function getJson($toArray=false)
    {
        if ($toArray) {
            return json_decode($this->json, true);
        }

        return $this->json;
    }

    public function getInFile()
    {
        return $this->inFile;
    }

    public function getOutPath()
    {
        return $this->outPath;
    }

}// end class JsonHelper